package org.adream.account.service;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.adream.account.util.WeixinUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;

@Service
public class WxService {
	private static final Logger logger = LoggerFactory.getLogger(WxService.class);
	
	@SuppressWarnings("unchecked")
	public String  wxLogin (HttpServletRequest request, HttpServletResponse response, String code) {
		if (StringUtils.isEmpty(code)) {
			logger.error("微信登录失败,code is null");
			throw new RuntimeException("登录失败!!");
		}
		String aTokenJsonString = WeixinUtil.getAccessToken(code);
		Map<String,Object> map = JSON.parseObject(aTokenJsonString,Map.class);
		
		String openid = String.valueOf(map.get("openid"));
		return "open:"+openid; 
	}

	
}
